package com.easy.modules.sysmgr.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.easy.common.dao.IBaseDao;
import com.easy.common.util.BeanUtils;
import com.easy.common.util.string.ClobUtil;
import com.easy.common.web.page.DataGrid;
import com.easy.common.web.page.Msg;
import com.easy.common.web.page.Pager;
import com.easy.modules.sysmgr.entity.NoticeEntity;
import com.easy.modules.sysmgr.service.NoticeServiceI;
import com.easy.modules.sysmgr.web.form.NoticeForm;

@Service
@Transactional
public class NoticeServiceImpl implements NoticeServiceI {

	@Autowired
	private IBaseDao<NoticeEntity> basedaoNotice;
	
	@Override
	public Msg save(NoticeForm form) {
		try {
			NoticeEntity entity = new NoticeEntity() ;
			BeanUtils.copyNotNullProperties(form, entity, new String[]{"content", "receiveMember"}) ;
			entity.setContent(ClobUtil.getClob(form.getContent())) ;
			entity.setReceiveMember(ClobUtil.getClob(form.getReceiveMember())) ;
 			entity.setCreated(new Date()) ;
			this.basedaoNotice.add(entity) ;
			
			return new Msg(true, "添加成功！") ;
		} catch (BeansException e) {
			return new Msg(false, "发生错误！") ;
		}
	}

	@Override
	public Msg delete(NoticeForm form) {
		try {
			if(null != form.getIds() && !"".equals(form.getIds())) {
				String[] ids = form.getIds().split(",") ;
				for (String id : ids) {
					this.basedaoNotice.delete(NoticeEntity.class, id) ;
				}
				return new Msg(true, "删除成功！") ;
			} else {
				return new Msg(false, "删除失败！") ; 
			}
		} catch (BeansException e) {
			return new Msg(false, "删除失败！") ;
		}
	}
	
	@Override
	public NoticeForm get(NoticeForm form) {
		Map<String, Object> params = new HashMap<String, Object>();
		String hql = "select t from NoticeEntity t where 1=1";
		hql = addWhere(hql, form, params) ;
		
		NoticeEntity entity = (NoticeEntity) this.basedaoNotice.queryObject(hql, params) ;
		if(null != entity) {
			NoticeForm pform = new NoticeForm();
			BeanUtils.copyProperties(entity, pform, new String[]{"content", "receiveMember"});
			pform.setContent(ClobUtil.getString(entity.getContent())) ;
			pform.setReceiveMember(ClobUtil.getString(entity.getReceiveMember())) ;
			return pform;
		} else {
			return null ;
		}
	}
	
	@Override
	public Msg edit(NoticeForm form) {
		try {
			NoticeEntity entity = this.basedaoNotice.load(NoticeEntity.class, form.getId());
			
			if(entity != null) {
				form.setCreated(new Date()) ;
				BeanUtils.copyNotNullProperties(form, entity, new String[] { "content", "receiveMember" });
				entity.setContent(ClobUtil.getClob(form.getContent())) ;
				entity.setReceiveMember(ClobUtil.getClob(form.getReceiveMember())) ;
				entity.setModifyDate(new Date()) ;
				this.basedaoNotice.update(entity) ;
			}
			
			return new Msg(true, "编辑成功！") ;
		} catch (BeansException e) {
			return new Msg(false, "编辑失败！") ;
		}
	}
	

	@Override
	public DataGrid datagrid(NoticeForm form) {
		
		List<NoticeForm> forms = new ArrayList<NoticeForm>();
		Pager<NoticeEntity> page = this.find(form) ;
		if (null != page && !page.getDataRows().isEmpty()) {
			for (NoticeEntity i : page.getDataRows()) {
				NoticeForm uf = new NoticeForm();
				BeanUtils.copyProperties(i, uf);
				uf.setContent(ClobUtil.getString(i.getContent())) ;
				uf.setReceiveMember(ClobUtil.getString(i.getReceiveMember())) ;
				
				forms.add(uf);
			}
		}
		
		DataGrid datagrid = new DataGrid();
		datagrid.setTotal(page.getTotal());
		datagrid.setRows(forms);
		return datagrid;
	}


	private Pager<NoticeEntity> find(NoticeForm form) {
		Map<String, Object> params = new HashMap<String, Object>();
		String hql = "select t from NoticeEntity t where 1=1";
		hql = addWhere(hql, form, params);
		return this.basedaoNotice.find(hql, params) ;
	}

	private String addWhere(String hql, NoticeForm form, Map<String, Object> params) {
		if (form.getId() != null && !"".equals(form.getId())) {
			hql += " and t.id=:id";
			params.put("id", form.getId());
		}
		return hql;
	}
}
